package com.mmlc.meimei.lichengkongjian;

import android.view.View;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.dialog.ResponseListener;
import com.mmlc.meimei.lichengkongjian.dialog.SanTianDialog;
import com.mmlc.meimei.lichengkongjian.dialog.TimeDialog;
import com.mmlc.network.filter.ShiyijianYuyueRequestFilter;
import com.mmlc.network.responsebean.ShiyijianResponseBean;
import com.mmlc.utils.ToastUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class YuyueShiyiActivity extends InterActivity {

    TextView editText1,editText2;

    @Override
    protected int initContentView() {
        return R.layout.activity_yuyue_shiyi;
    }

    @Override
    protected void initView() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.commit).setOnClickListener(this);
        editText1= (TextView) findViewById(R.id.riqi);
        editText2= (TextView) findViewById(R.id.shijian);
        editText1.setOnClickListener(this);
        editText2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.commit:
                commmit();
                break;
            case R.id.riqi:
                SanTianDialog sanTianDialog=new SanTianDialog();
                sanTianDialog.setListener(new ResponseListener<String>() {
                    @Override
                    public void response(String data) {
                        editText1.setText(data);
                    }
                });
                sanTianDialog.show(getSupportFragmentManager(),"");
                break;
            case R.id.shijian:
                TimeDialog timeDialog=new TimeDialog();
                timeDialog.setInitDateAndListner("13:00", "时间", new ResponseListener<String>() {
                    @Override
                    public void response(String data) {
                        editText2.setText(data);
                    }
                });
                timeDialog.show(getSupportFragmentManager(),"");
                break;
        }
    }
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat format2=new SimpleDateFormat("HH:mm");
    void commmit(){
        String riqi=editText1.getText().toString().trim();
        String shijian=editText2.getText().toString().trim();
        if(!yanzheng(riqi,shijian)){
            return;
        }
        ShiyijianYuyueRequestFilter s = new ShiyijianYuyueRequestFilter(this);
        s.requestBean.time=riqi+" "+shijian;
        s.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ShiyijianResponseBean>() {
            @Override
            public void onSuccess(final ShiyijianResponseBean result) {
                ToastUtils.showToast(result.getMessage());
                if(result.getCode()==0){
                    finish();
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    boolean yanzheng(String riqi,String shijian){
        boolean convertSuccess=true;
        try {
            format.setLenient(false);
            format.parse(riqi);
            format2.setLenient(false);
            format2.parse(shijian);
        } catch (ParseException e) {
            ToastUtils.showToast("请正确输入日期(如:2018-1-10)或时间格式(如:15:30)");
            convertSuccess=false;
        }
        return convertSuccess;
    }
}
